import Vue from 'vue'
import VueRouter from 'vue-router'

import Index from '@/views/IndexPage.vue'
import Home from '@/views/HomePage.vue'
import UserManage from '@/views/UserManage.vue'
import Goods from '@/views/GoodsMessage.vue'
import Cost from '@/views/CostMessage.vue'
import Login from '@/views/Login.vue'
import Register from '@/views/Register.vue'
import Client from '@/views/ClientMessage.vue'
import Relocation from '@/views/RelocationMessage.vue'
import Warehouse  from "@/views/Warehouse.vue";
import Cargo from '@/views/CargoMessage.vue'
import Int from '@/views/IntMessage.vue'
import Out from '@/views/OuttMessage.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    name: '登录',
    component: Login,
    meta: {
      requireAuth: false
    }
  },
  {
    path: '/register',
    name: '注册',
    component: Register,
    meta: {
      requireAuth: false
    }
  },
  {
    path: '/',
    name: '领导',
    component: Index,
    meta: {
      requireAuth: true
    },
    children: [
      {
        path: '/home',
        name: '首页',
        component: Home,
      },
      {
        path: '/user',
        name: '用户管理',
        component: UserManage,
      },
      {
        path: '/goods',
        name: '货物信息',
        component: Goods,
      },
      {
        path: '/cost',
        name: '收费详情',
        component: Cost,
      },
      {
        path: '/client',
        name: '客户信息',
        component: Client,
      },
      {
        path: '/relocation',
        name: '移库记录表',
        component: Relocation,
      },
      {
        path: '/warehouse',
        name: '仓库信息',
        component: Warehouse,
      },
      {
        path: '/cargo',
        name: '货物信息',
        component: Cargo,
      },
      {
        path: '/int',
        name: '入库记录表',
        component: Int,
      },
      {
        path: '/out',
        name: '出库记录表',
        component: Out,
      },
    ],
  },
]

const router = new VueRouter({
  // mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  if (to.meta.requireAuth) {
    // 需要登录
    const token = localStorage.getItem('token')
    if (token) {
      next()
    } else {
      next({
        path: '/login',
        query: { redirect: to.fullPath }  // 将要跳转的路由path作为参数，登录成功后跳转到该路由
      })
    }
  } else {
    next()
  }
})

export default router
